﻿// -----------------------------------------------------------------------
// <copyright file="PositionData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Position
    /// </summary>
    public class PositionData
    {
        /// <summary>
        /// Get Position By Id
        /// </summary>
        /// <param name="positionsId">Param positionsId</param>
        /// <returns>Return Position</returns>
        public PositionEN GetPositionById(int positionsId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                PositionEN position = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsGetById, positionsId);
                while (reader.Read())
                {
                    position = new PositionEN();

                    if (!reader.IsDBNull(0))
                    {
                        position.PositionsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        position.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        position.Name = reader.GetString(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return position;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Position
        /// </summary>
        /// <returns>Return list all Position</returns>
        public IList<PositionEN> GetPositions()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<PositionEN> lstPosition = new List<PositionEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsGetAll);
                while (reader.Read())
                {
                    PositionEN position = new PositionEN();

                    if (!reader.IsDBNull(0))
                    {
                        position.PositionsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        position.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        position.Name = reader.GetString(2);
                    }

                    lstPosition.Add(position);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstPosition;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Position
        /// </summary>
        /// <param name="position">Param Position</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(PositionEN position)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Position.Code, position.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Position.Name, position.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Position
        /// </summary>
        /// <param name="position">Param Position</param>
        public void Update(PositionEN position)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Position.PositionsId, position.PositionsId),
                                    new SqlParameter(Constants.SqlParamerterName.Position.Code, position.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Position.Name, position.Name)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Position by positionsId
        /// </summary>
        /// <param name="positionsId">param positionsId</param>
        public void Delete(int positionsId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Position.PositionsId, positionsId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="positionsId">Param positionsId</param>
        /// <param name="code">Param code</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int positionsId, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Position.PositionsId, positionsId),
                    new SqlParameter(Constants.SqlParamerterName.Position.Code, code)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}